package com.stud.repository;

import com.stud.repository.mapper.ClientMapper;
import com.stud.rmi.Client;

public class ClientRepositoryImpl implements ClientRepository {

    private static final String INSERT_CLIENT = "insert into client (first_name, last_name) values (?, ?)";
    private static final String DELETE_CLIENT = "delete from client where first_name=? and last_name=?";
    private static final String SELECT_CLIENT = "select first_name, last_name from client where first_name=? and last_name=?";

    private final JdbcTemplate jdbcTemplate;

    public ClientRepositoryImpl(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public void save(Client client) {
        jdbcTemplate.update(
                INSERT_CLIENT,
                client.getFirstName(),
                client.getLastName()
        );
    }

    @Override
    public void delete(Client client) {
        jdbcTemplate.update(
                DELETE_CLIENT,
                client.getFirstName(),
                client.getLastName()
        );
    }

    @Override
    public Client getClient(Client client) {
        return jdbcTemplate.queryForObject(
                SELECT_CLIENT,
                new ClientMapper(),
                client.getFirstName(),
                client.getLastName()
        );
    }
}
